package com.wushi.lenovo.asia5b.app_ui.credit.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.ListPopupWindow;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.mvp.base.BaseMvpActivity;
import com.mvp.base.BaseSimpleFragment;
import com.mvp.base.model.ResultModel;
import com.mvp.utils.ToastUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.credit.mvpview.TransHisListView;
import com.wushi.lenovo.asia5b.app_di.credit.presenter.TransHisListPresenter;
import com.wushi.lenovo.asia5b.app_event.DownPdfEvent;
import com.wushi.lenovo.asia5b.app_model.credit.BaseHisModel;
import com.wushi.lenovo.asia5b.app_model.credit.TransactionHisModel;
import com.wushi.lenovo.asia5b.app_model.credit.bean.TransactionHisType;
import com.wushi.lenovo.asia5b.app_model.other.PopupItemTextIml;
import com.wushi.lenovo.asia5b.app_ui.credit.adapter.TransHisListAdapter;
import com.wushi.lenovo.asia5b.app_ui.credit.fragment.DownPdfDF;
import com.wushi.lenovo.asia5b.app_utils.eye.PopupListAdapter;
import com.wushi.lenovo.asia5b.app_utils.eye.PopupWindowHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2019/1/20.
 * 充值历史记录列表
 */
public class TransHisListActivity extends BaseMvpActivity<TransHisListView, TransHisListPresenter> implements TransHisListView {
    @BindView(R.id.tv_his_filtertime)
    TextView tv_his_filtertime;
    @BindView(R.id.tv_transhis_status)
    TextView tv_transhis_status;
    @BindView(R.id.recyclerview_transhislist)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_title_titletext)
    TextView tv_title_titletext;
    private String lastOneWeek;
    private String lastOneMonth;
    private String pastSixMonth;
    private String lastTwelveMonth;
    private TransHisListAdapter mAdapter;
    private final List<MultiItemEntity> mDatas = new ArrayList<>();
    private ListPopupWindow popupWindowFilter;
    private PopupListAdapter<PopupItemTextIml> filterAdapter;


    private TransactionHisType transactionHisType = TransactionHisType.all;
    private TextView emptyView;
    private final Map<String, String> filterMap = new HashMap<>();

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_transhislist;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        transactionHisType = (TransactionHisType) getIntent().getSerializableExtra("TransactionHisType");
        initTransFilterTypeText();
        d("--->>>>>>>>---lastOneWeekL" + lastOneWeek);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        List<PopupItemTextIml> filterList = new ArrayList<>();
        filterList.add(new PopupItemTextIml(lastOneWeek));
        filterList.add(new PopupItemTextIml(lastOneMonth));
        filterList.add(new PopupItemTextIml(pastSixMonth));
        filterList.add(new PopupItemTextIml(lastTwelveMonth));
        filterMap.clear();
        filterMap.put(lastOneWeek, "last_one_week");
        filterMap.put(lastOneMonth, "last_one_month");
        filterMap.put(pastSixMonth, "past_six_month");
        filterMap.put(lastTwelveMonth, "last_twelve_month");
        filterAdapter = new PopupListAdapter<>(this, R.layout.item_popupitem_transhis_adapter, filterList);
        popupWindowFilter = PopupWindowHelper.getListPopupWindow(this, filterAdapter
                , tv_his_filtertime, ListPopupWindow.WRAP_CONTENT);
        filterAdapter.setSelectPosition(0);
        tv_his_filtertime.setText(filterAdapter.getSelectItemText());

        mAdapter = new TransHisListAdapter(mDatas);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);

        emptyView = (TextView) View.inflate(this, R.layout.empty_transhislist, null);
//        mAdapter.setEmptyView(emptyView);

        tv_transhis_status.setText(transactionHisType == TransactionHisType.all ? R.string.withdraw_balance : R.string.transhistory_status);
    }

    private void initTransFilterTypeText() {
        lastOneWeek = getString(R.string.transhistory_last_one_week);
        lastOneMonth = getString(R.string.transhistory_last_one_month);
        pastSixMonth = getString(R.string.transhistory_past_six_month);
        lastTwelveMonth = getString(R.string.transhistory_last_twelve_month);
        switch (transactionHisType) {
            case topup:
                tv_title_titletext.setText(R.string.transhistory_title_topup);
                break;
            case credittrans:
                tv_title_titletext.setText(R.string.transhistory_title_credit);
                break;
            case withdraw:
                tv_title_titletext.setText(R.string.transhistory_title_withdraw);
                break;
            case all:
                tv_title_titletext.setText(R.string.transhistory_title_all);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initData() {
        getTransactionHisList();
    }

    private void getTransactionHisList() {
        getPresenter().getTransactionHisList(filterMap.get(tv_his_filtertime.getText().toString()), transactionHisType);
    }

    @Override
    protected void initEvent() {
        popupWindowFilter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                popupWindowFilter.dismiss();
                filterAdapter.setSelectPosition(position);
                tv_his_filtertime.setText(filterAdapter.getSelectItemText());
                getTransactionHisList();
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                BaseHisModel item = (BaseHisModel) mAdapter.getItem(position);
                if (item instanceof TransactionHisModel) {
                    TransactionHisModel model = (TransactionHisModel) item;
                    if (TextUtils.equals("Opening Balance", model.getType())) {
                        return;
                    }
                }
//                DownPdfDialog dialog = new DownPdfDialog(TransHisListActivity.this);
//                dialog.show();
//                dialog.setModel(item);
//                Bundle bundle = new Bundle();
//                bundle.putSerializable("data", item);
                DownPdfDF pdfDF = (DownPdfDF) BaseSimpleFragment.sNewStance(TransHisListActivity.this, DownPdfDF.class);
                pdfDF.mDatas.add(item);
                pdfDF.downpdf_title = getString(R.string.transhistory_downloadtitle_topup);
                pdfDF.show(getSupportFragmentManager(), "DownPdfDF");
            }
        });
    }

    @OnClick({
            R.id.tv_his_filtertime
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_his_filtertime:
                PopupWindowHelper.showPopupWindow(popupWindowFilter);
                break;
            default:
                break;
        }
    }

    @Override
    public <T extends MultiItemEntity> void getTransactionHisListResult(ResultModel<List<T>> listResultModel, String filter) {
        mDatas.clear();
        if (listResultModel.isSuccess() && listResultModel.getData() != null) {
            mDatas.addAll(listResultModel.getData());
        }
        mAdapter.notifyDataSetChanged();
        mAdapter.setEmptyView(emptyView);
    }

    @Override
    public void downloadPdfResult(String s) {
        if (!TextUtils.isEmpty(s)) {
            String toast = getString(R.string.save_success) + ":" + s;
            ToastUtil.showToast(toast);
        } else {
            ToastUtil.showToast(getString(R.string.save_fail));
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDownPdfEvent(DownPdfEvent event) {
        if (event != null && !TextUtils.isEmpty(event.reference_number)) {
            for (int i = 0; i < mDatas.size(); i++) {
                MultiItemEntity multiItemEntity = mDatas.get(i);
                if (multiItemEntity instanceof BaseHisModel) {
                    BaseHisModel model = (BaseHisModel) multiItemEntity;
                    if (TextUtils.equals(model.getReference_number(), event.reference_number)) {
                        mAdapter.notifyItemChanged(i);
                        break;
                    }
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
